import cv2
import numpy as np

img = cv2.imread('meinv.png')

print(img)
px = img[100, 100]
#像素 BGR(Blue, Green, Red values) [157 166 200]
print(px)
# accessing only blue pixel
blue = img[100, 100, 0]
print(blue)

# accessing RED value
print(img.item(10, 10, 2))

# modifying RED value
img.itemset((10, 10, 2), 100)
print(img.item(10, 10, 2))

# shape(340, 353, 3)如果是灰度图，则shape只会显示行列
print(img.shape)
# Total number of pixels
print(img.size)
# Image datatype
print(img.dtype)

# Image ROI(region of images)
part = img[200:300, 200:300]
# 移花接木
img[0:100, 50:150] = part
cv2.imwrite('meinv_1.png', img)

# Splitting and Merging Image Channels
# cv2.split() is a costly operation (in terms of time), so only use it if necessary.
# Numpy indexing is much more efficient and should be used if possible.
"""The B,G,R channels of an image can be split into their individual planes when needed.
 Then, the individual channels can be merged back together to form a BGR image again. 
 This can be performed by:"""
b,g,r = cv2.split(img)
img = cv2.merge((b,g,r))
# or
b = img[:,:,0]
#or  make all the red pixels to zero,
img[:,:,2] = 0
cv2.imwrite('meinv_2.png', img)
